﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LogManager.Windows
{
    public static class WindowsEventDetailHelper
    {
        public static IEnumerable<string> GetUndefinedEventProperties(this EventRecord record)
        {
            return record.Properties.Count > 0 ? record.Properties.Select(ep => ep.Value.ToString()) : null;
        }

        public static IDictionary<string,string> GetEventProperties(this EventRecord record)
        {
            //If common event log let it be
            if (record.Properties.Count != 1)
            {
                return null;
            }

            var properties = new Dictionary<string, string>();
            
            foreach (string row in record.Properties.First().Value.ToString().Split('\n'))
            {
                int index = row.IndexOf(':');
                if (index > 0)
                {
                    var regex = new Regex(@"[ ]{2,}");

                    //Left side
                    string key = regex.Replace(row.Substring(0, index).Trim(), @" ");
                    //Right side
                    string value = row.Substring(index + 1).Trim();

                    if (!string.IsNullOrEmpty(key))
                    {
                        properties[key] = value;
                    }
                }
            }

            return properties;
        }
    }
}
